#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         Tody Kwok
 Date:			2019-04-10
 Script Function:
	this script is for ME Keyboard backlight testing.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#pragma compile(x64, true)
#pragma compile(FileVersion, 1.0.0.0)
#pragma compile(ProductVersion, 1.0.0.0)
#pragma compile(ProductName, "OemServiceUI.exe")
#pragma compile(LegalCopyright, '(c) Tody, 2019 T-ware Inc.')
#pragma compile(FileDescription, ME Keyboard backlight testing tool)
#pragma compile(Icon, C:\Program Files (x86)\AutoIt3\Icons\MyAutoIt3_Green.ico)

#RequireAdmin
#NoTrayIcon

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Global $batFile
Global $binFile=@ScriptDir&"\OemServiceWinApp.exe"
Global $webFile=@ScriptDir&"\data\temp.html"
Global $idPN
Global $idSN

main()

Func WriteBatch($id)
   $batFile=@ScriptDir&"\data\"&GUICtrlRead($idSN)&".bat"
   If FileExists($batFile) Then FileDelete($batFile)
   Local $hFileOpen = FileOpen($webFile)
   If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
	 EndIf
	 FileWriteLine($batFile, "	cd /d %~dp0")
  While 1
        $line= FileReadLine($hFileOpen)
        If  @error = -1 Then ExitLoop
		 if StringInStr($line, "OemServiceWinApp") Then FileWriteLine($batFile, $line)
		 if StringInStr($line, "AMIDEWINx64") Then FileWriteLine($batFile, $line)
		 if StringInStr($line, "FGM") OR StringInStr($line, "FBM") Then
			ControlSetText ("", "", $idPN, StringReplace(StringRight($line, 19), '"',''))
		 EndIf
   Wend
	  FileWriteLine($batFile, "	shutdown -r -t 5")
   FileClose($hFileOpen)
EndFunc

Func main()
	GUICreate("RMA_TOOL v1.0", 300,120) ; will create a dialog box that when displayed is centered
	;If FileExists($batFile) Then FileDelete($batFile)
   GUICtrlCreateLabel("序号",0,10)
   $idSN=GUICtrlCreateInput("",30,5,155,20)
   GUICtrlCreateLabel("盘符",0,35)
   $idLtr=GUICtrlCreateInput("",30,35,155,20)
   GUICtrlCreateLabel("料号",0,65)
   $idPN=GUICtrlCreateInput("",30,65,155,20)
   GUICtrlCreateLabel("网盘",0,95)
   $idDisk=GUICtrlCreateInput("",30,95,155,20)

   Local $idStart = GUICtrlCreateButton("开始制作", 200, 5, 85, 110)

   GUISetState(@SW_SHOW) ; will display an  dialog box

   DriveMapAdd ("X:", "\\10.100.160.32\mp_biosec", $DMA_AUTHENTICATION)   ;; mapping bios ec drive

   Local $idMsg
	; Loop until the user exits.
	While 1
		$idMsg = GUIGetMsg()
		Select
		 case $idMsg = $GUI_EVENT_CLOSE
			DriveMapDel ("X:")
			If FileExists($webFile) Then FileDelete($webFile)
			ExitLoop

		 case $idMsg = $idStart
			$aArray=DriveGetDrive($DT_REMOVABLE)  ;;; here is for Select drive for u Disk
			if @error = 1 Then
			   $selLtr=FileSelectFolder ("选择U盘符","D:")
			Else
			   $selLtr=FileSelectFolder ("选择U盘符",$aArray[1])
			EndIf

		    ControlSetText ("", "", $idLtr, StringLeft($selLtr,3))

			If FileExists($webFile) Then FileDelete($webFile)  ;;delete file
		    if FileExists($batFile) Then FileDelete($batFile)  ;; delete file

		    $snID=GUICtrlRead($idSN)  ;; read sn

			if StringLen($snID) > 10 Then
			  GUICtrlSetState($idStart, $GUI_DISABLE)  ;; disable start button
			  InetGet("http://10.100.160.166/getworkorder.php?act=q&sn="&StringStripWS($snID, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES), $webFile)   ;; download file from TE website by sn
			  RunWait("c:\windows\system32\format.com "&StringLeft(GUICtrlRead($idLtr),2)&" /y /q")
			  RunWait(@ScriptDir&"\data\7z.exe x -o"&$selLtr&" "&@ScriptDir&"\data\efi.7z -y")  ;; extract basic efi file for efi boot

			  WriteBatch($webFile)   ;; write batch files with sn .bat
			  DirCopy(@ScriptDir&"\data\oem\", StringLeft(GUICtrlRead($idLtr),3),1)
			  $ret=FileCopy("X:\"&GUICtrlRead($idPN)&"\bios.7z", StringLeft(GUICtrlRead($idLtr),3), $FC_OVERWRITE)  ;; copy files to U disk by PN
			  if $ret = 1 Then
				  MsgBox(64, "成功", "BIOS拷贝成功",1)
			   Else
				  MsgBox(16, "成功", "BIOS拷贝失败")
			   EndIf

			  $ret=FileCopy($batFile, GUICtrlRead($idLtr), 1) ;; copy files to U disk
			  if $ret = 1 Then
				  MsgBox(64, "成功", "烧值文件拷贝成功",1)
			   Else
				  MsgBox(16, "成功", "烧值文件拷贝失败")
			   EndIf

			  RunWait(@ScriptDir&"\data\7z.exe e -o"&$selLtr&" "&$selLtr&"bios.7z -y")  ;; extract bios.7z files to U disk
			  If FileExists($batfile) Then FileDelete($batFile)   ;; delete local sn .bat file
			  GUICtrlSetState($idStart, $GUI_ENABLE)   ;; enable start button
			  MsgBox(64, "成功", "U盘制作成功！可以更新BIOS或烧值")
		    Else
			  MsgBox(16, "错误", "序列号有问题")
		    EndIf

		EndSelect
	   ControlSetText ("", "", $idDisk, DriveMapGet ("x:"))
	WEnd
EndFunc   ;==>end main
